package com.itya.weatherback.utils;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;

import java.util.Date;

@Data
public class Result<T> {
    private int code;
    private String message;
    private T data;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date timestamp;

    // 构造函数
    public Result() {
        this.timestamp = new Date();
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.timestamp = new Date();
    }

    // 成功状态的静态方法
    public static <T> Result<T> success(T data) {
        return new Result<>(200, "success", data);
    }

    // 失败状态的静态方法
    public static <T> Result<T> error(int code, String message) {
        return new Result<>(code, message, null);
    }


    public static <T> Result<T> error(String message) {
        return error(500, message);
    }

}
